<?php
/**
 * Created by PhpStorm.
 * User: aiChenK
 * Date: 2019-02-12
 * Time: 13:23
 */

namespace Services\Msg\Core\Notice;

use Helpers\Di;
use Services\Login\Login;
use Services\Msg\Models\NoticeTarget;

class Operator
{

    /**
     * 标记已读
     *
     * @param $ids
     * @throws \Exception
     *
     * create by ck 20190130
     */
    public function read($ids)
    {
        if (is_numeric($ids)) {
            $ids = [$ids];
        }
        $ids = array_filter($ids);
        if (!$ids) {
            throw new \Exception('缺少消息id');
        }
        $result = Di::get()->db->update(
            'XHRemind_NoticeTarget',
            ['isRead', 'updateAt', 'updateBy'],
            [1, time(), Login::getId()],
            sprintf(
                'noticeId IN (%s) AND targetId = "%d" AND isDel = 0 AND isRead = 0',
                implode(',', $ids),
                Login::getId()
            )
        );
        if (!$result) {
            throw new \Exception('标记失败');
        }
    }

    /**
     * 标记消息失效
     *
     * @param string $module
     * @param string $subModule
     * @param $extraId
     * @param string $operate
     * @return bool
     * @throws \Exception
     *
     * create by ck 20190124
     * modify by ck 20190214
     */
    public function scrap(string $module, string $subModule, $extraId, string $operate = '')
    {
        $condition = [
            "module = '{$module}'",
            "subModule = '{$subModule}'",
            "extraId = '{$extraId}'",
            "operate = '{$operate}'",
            "isDel = 0",
            "isScrap = 0"
        ];
        $result = Di::get()->db->update(
            'XHRemind_Notice',
            ['isScrap', 'updateAt', 'updateBy'],
            [1, time(), Login::getId()],
            implode(' AND ', $condition)
        );
        if (!$result) {
            throw new \Exception('标记失败');
        }
        return true;
    }

    /**
     * 所有关联消息失效，无视operate
     *
     * @param string $module
     * @param string $subModule
     * @param $extraId
     * @return bool
     * @throws \Exception
     *
     * create by ck 20190214
     */
    public function scrapAllOperate(string $module, string $subModule, $extraId)
    {
        $condition = [
            "module = '{$module}'",
            "subModule = '{$subModule}'",
            "extraId = '{$extraId}'",
            "isDel = 0",
            "isScrap = 0"
        ];
        $result = Di::get()->db->update(
            'XHRemind_Notice',
            ['isScrap', 'updateAt', 'updateBy'],
            [1, time(), Login::getId()],
            implode(' AND ', $condition)
        );
        if (!$result) {
            throw new \Exception('标记失败');
        }
        return true;
    }

    /**
     * 删除个人消息
     *
     * @param array $ids
     * @return bool
     * @throws \Exception
     *
     * create by ck 20190130
     */
    public function del(array $ids)
    {
        if (!$ids) {
            throw new \Exception('缺少消息id');
        }
        if (!NoticeTarget::deleteById($ids)) {
            throw new \Exception('删除失败');
        }
        return true;
    }
}
